package com.hemerasolutions.v2trainer.model.users;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.library.Library;
import com.hemerasolutions.v2trainer.model.multimedia.Photo;
import com.hemerasolutions.v2trainer.model.security.AccessRole;

@Entity
public class User extends BaseModel {
 
	private static final long serialVersionUID = 1L;

    @Column(length = 50,unique=true)
	private String email;

    @Column(length = 15)
	private String password;

    @Column(length = 22)
	private String phone;

    @Column
    private int userType;
    
    @Column
	private boolean active;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date expireOn;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date createdAt;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
	private Date lastChange;

	@OneToOne 
    private User changedBy;

    @OneToOne 
    private Photo photo;
    
    @OneToMany(mappedBy="owner", cascade = CascadeType.ALL,fetch=FetchType.LAZY, orphanRemoval = true)
	private List<Library> librarys;
    
    @ManyToMany
	private List<AccessRole> roles ;        

    public final static int USERTYPE_ADMIN = 1;
    
    public final static int USERTYPE_ACADEMY = 2;

    public final static int USERTYPE_TEACHER = 3;

    public final static int USERTYPE_PT = 4;

    public final static int USERTYPE_ATHLETE = 5;
    
    
    public User() {
		super();
		this.roles = new ArrayList<AccessRole>();
		this.librarys = new ArrayList<Library>();
		this.createdAt = new Date();
		this.lastChange = new Date();
		this.active		= true;
    }


	public int getUserType() {
		return userType;
	}

	public void setUserType(int userType) {
		this.userType = userType;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Photo getPhoto() {
		return photo;
	}

	public void setPhoto(Photo photo) {
		this.photo = photo;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	
	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Date getExpireOn() {
		return expireOn;
	}

	public void setExpireOn(Date expireOn) {
		this.expireOn = expireOn;
	}

	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}

	public Date getLastChange() {
		return lastChange;
	}

	public void setLastChange(Date lastChange) {
		this.lastChange = lastChange;
	}

	public User getChangedBy() {
		return changedBy;
	}

	public void setChangedBy(User changedBy) {
		this.changedBy = changedBy;
	}

	public List<AccessRole> getRoles() {
		return roles;
	}

	public void setRoles(List<AccessRole> roles) {
		this.roles = roles;
	}
	

	public List<Library> getLibrarys() {
		return this.librarys;
		
	}

	public void setLibrarys(List<Library> librarys) {
//* Put the default library at the top of list before any operation		
		for(Library lib: librarys){
			if(lib.isDefaultLibrary()){
				librarys.remove(lib);
				librarys.add(0, lib);
				break;
			}
		}
		
		this.librarys = librarys;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", email=" + email + "]";
	}

}
 
